package com.hsy.swu.hsyutils

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Window
import android.view.animation.DecelerateInterpolator
import androidx.viewbinding.ViewBinding

open class HsyDialog<VB : ViewBinding>(
    context: Context,
    private val onCreateCallback: (VB, Dialog) -> Unit,
    private val isAnimateDismiss: Boolean = true, // loadingDialog设为 false，直接消失
) : Dialog(context) {
    protected val mBinding = createViewBinding<VB>(layoutInflater)!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        mBinding.setFont()
        setContentView(mBinding.root)
        onCreateCallback(mBinding, this)
    }

    override fun show() {
        mBinding.root.animate()
            .scaleX(0.2f)
            .scaleY(0.2f)
            .alpha(0f)
            .setDuration(0L)
            .start()
        super.show()
        mBinding.root.animate()
            .scaleX(1f)
            .scaleY(1f)
            .alpha(1f)
            .setInterpolator(DecelerateInterpolator())
            .setDuration(250L)
            .start()
    }

    override fun dismiss() {
        if (isAnimateDismiss) {
            mBinding.root.animate()
                .scaleX(0.2f)
                .scaleY(0.2f)
                .alpha(0f)
                .setInterpolator(DecelerateInterpolator())
                .setDuration(250L)
                .withEndAction { super.dismiss() }
                .start()
        } else {
            super.dismiss()
        }
    }
}